/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var xmlHttp
var elementId
var cross = '<img src="../templates/cross.png" align="center"/>'
var tick = '<img src="../templates/tick.png" align="center"/>'
var url = "../project/registerAdministrator.php";


function testName(str){
    elementId = "nameVal";
    xmlHttp = GetXmlHttpObject();
    if(xmlHttp == null){
        alert("Your browser does not support AJAX!");
        return;
    }
    xmlHttp.onreadystatechange = nameStateChanged;
    xmlHttp.open("POST", url, true);
    var name = 'name='+str;
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send(name);
}

function testPassword(str){
    elementId = "passVal";
    xmlHttp = GetXmlHttpObject();
    if(xmlHttp == null){
        alert("Your browser does not support AJAX!");
        return;
    }
    xmlHttp.onreadystatechange = passStateChanged;
    xmlHttp.open("POST", url, true);
    var pass = 'password='+str;
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send(pass);
}

function testMail(str){
    elementId = "mailVal";
    xmlHttp = GetXmlHttpObject();
    if(xmlHttp == null){
        alert("Your browser does not support AJAX!");
        return;
    }
    xmlHttp.onreadystatechange = mailStateChanged;
    xmlHttp.open("POST", url, true);
    var pass = 'email='+str;
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send(pass);
}

function nameStateChanged() {
    if (xmlHttp.readyState == 4){
        var result;
        var response = JSON.parse(xmlHttp.responseText);
        var image;
        if(response == 'Ok!')
            image = tick;
        else
            image = cross;
        result = image;
        document.getElementById('nameVal').innerHTML = result;
    }
}

function mailStateChanged() {
    if (xmlHttp.readyState == 4){
        var result;
        var response = JSON.parse(xmlHttp.responseText);
        var image;
        if(response == 'Ok!')
            image = tick;
        else
            image = cross;
        result = image;
        document.getElementById('mailVal').innerHTML = result;
    }
}

function passStateChanged() {
    if (xmlHttp.readyState == 4){
            var response = JSON.parse(xmlHttp.responseText);
            var result;
            var complexity = response;
            result = 'Password security: ';
            if(complexity == 1)
                result += 'very low';
            else if (complexity == 2)
                result += 'low';
            else if (complexity == 3)
                result += 'normal';
            else
                result += 'good';
            document.getElementById('passVal').innerHTML = result;

        }
}

function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        xmlHttp = new XMLHttpRequest();
    } catch(e){
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e){
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}


